package com.gimi.cloud.bbp.manage.service.sys.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.sys.SysRoleDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.sys.SysRoleDTO;
import com.gimi.cloud.bbp.entity.sys.SysRoleEntity;
import com.gimi.cloud.bbp.manage.service.sys.SysRoleService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色相关接口实现类
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-25
 **/
@Service
public class SysRoleServiceImpl implements SysRoleService{
    private static final Logger logger = LoggerFactory.getLogger(SysRoleServiceImpl.class);
    private SysRoleDao sysRoleDao;

    @Autowired
    public SysRoleServiceImpl(SysRoleDao sysRoleDao) {
        this.sysRoleDao = sysRoleDao;
    }

    @Override
    public List<SysRoleDTO> queryList(SysRoleDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "select role.* from sys_role role where role.is_delete = 0 and role.ro_name like '%'||:roName||'%'";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        return sysRoleDao.selectListDTOBySql(param);
    }

    @Override
    public SysRoleDTO load(SysRoleDTO dto, Object id) throws Exception {
        SysRoleEntity sysRoleEntity = sysRoleDao.queryBy("ro_id",id);
        if(null == sysRoleEntity){
            return null;
        }
        SysRoleDTO roleDTO = new SysRoleDTO();
        ReflectUtils.copyPropertiesIgnoreNull(sysRoleEntity,roleDTO);
        return roleDTO;
    }

    @Override
    public SysRoleDTO saveOrUpdate(SysRoleDTO dto) throws Exception {
        SysRoleEntity role = null;
        if (null != dto.getRoId()) {
            role = sysRoleDao.queryBy("ro_id", dto.getRoId());
            if (null == role) {
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取角色id为" + dto.getRoAlias() + "的角色信息，更新失败");
            }
        } else {
            role = new SysRoleEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNull(dto, role);
        if(StringUtils.isNotBlank(role.getRoName())){
            int existCount = 0;
            if(null == role.getRoId()){
                existCount = sysRoleDao.queryCountBy("ro_name", role.getRoName());
            }else{
                existCount = sysRoleDao.queryCountByExclude("ro_name", role.getRoName(),"ro_id",role.getRoId());
            }
            if(existCount>0){
                throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"已存在名称为"+role.getRoName()+"的角色");
            }
        }
        if (null != dto.getRoId()) {
            sysRoleDao.baseUpdateByPrimaryKey(role);
        } else {
            sysRoleDao.baseInsert(role);
        }
        ReflectUtils.copyPropertiesIgnoreNull(role, dto);
        return dto;
    }

    @Override
    public int delete(SysRoleDTO dto, Object id, boolean realDel) throws Exception {
        SysRoleEntity role = sysRoleDao.queryBy("ro_id", id);
        if (null == role) {
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取角色id为" + id + "的角色信息，删除失败");
        }
        //非物理删除
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNull(dto,role);
            role.setIsDelete(1);
            return sysRoleDao.baseUpdateByPrimaryKey(role);
        }
        return sysRoleDao.deleteByPrimaryKey(id);
    }
}
